// pages/tixian/tixian.js
var api = require('../../api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myInfo: null,
    tixian:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    var access_token = wx.getStorageSync("access_token");
    app.request({
      url: api.user.get_brokerage,
      data: { userId: access_token },
      success: function (res) {
        if (res.code == 200) {
          setTimeout(function () {
            wx.hideLoading();
          }, 1000);
          setTimeout(function () {
            page.setData({
              options: {},
            });
          }, 1);
      
        }
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    page.setData({
      store: wx.getStorageSync('store'),
      myInfo: wx.getStorageSync('my_info')
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputedit: function (e) {
    this.data.tixian = e.detail.value.tixian;
    this.setData({
      tixian: e.detail.value
  });
},
  tixianSubmit: function (e) {
    var page = this;
    wx.requestSubscribeMessage({
      tmplIds: ['F6oyNZhjaQYOMWdGJcrd6wne7nf0qTr2gH7vaIMmX5k',
      'qZOpyTxHMMB9EibgkUtm5mv9_lkQ09PYvwzdw6-pzqw'],
      success(res) {
        page.formSubmit(e);
      }
    })
},
  formSubmit: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_info = wx.getStorageSync("my_info");
    console.log(my_info)
    //获取支付数据
    app.request({
      url: api.order.tixian,
      data: {
        money: page.data.tixian,
        payType: 2,
      },
      success: function (res) {
        console.log(res);

        if (res.code == 200) {
          //发起支付
          wx.showModal({
            title: "提示",
            content: "提现成功",
            showCancel: false,
            confirmText: "确认",
            success: function (res) {
                wx.redirectTo({
                  url: "/pages/user/user?status=-1",
                });
              
            }
          });
          return;
        }else if(res.code == 500){
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            confirmText: "确认",
            success: function (res) {
                wx.redirectTo({
                  url: "/pages/user/user?status=-1",
                });
              
            }
          });
          return;
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
          return;
        }
      }
    });
  }
})